package com.jeasonzhao.commons.parser.expression;

import com.jeasonzhao.commons.parser.lex.BaseTokenizer;
import com.jeasonzhao.commons.parser.lex.KnownConfiguration;
import com.jeasonzhao.commons.parser.lex.LexException;
import com.jeasonzhao.commons.parser.lex.LexToken;
import com.jeasonzhao.commons.utils.Algorithms;

public class CSSParser
{
    public CSSParser()
    {
        super();
    }

    public static String refinePart(String x)
    {
        try
        {
            return generateFromPart(parsePart(x));
        }
        catch(SyntaxException ex)
        {
            return x;
        }
        catch(LexException ex)
        {
            return x;
        }
    }

    public static String generateFromPart(java.util.Hashtable<String,String> map)
    {
        if(null == map || map.size() < 1)
        {
            return null;
        }
        else
        {
            StringBuffer buf = new StringBuffer();
            for(java.util.Map.Entry<String,String> e : map.entrySet())
            {
                buf.append(e.getKey() + ":" + e.getValue() + ";");
            }
            return buf.toString();
        }
    }

    //font-family: Verdana, sans-serif;
    public static java.util.Hashtable<String,String> parsePart(String str)
        throws LexException,SyntaxException
    {
        if(Algorithms.isEmpty(str))
        {
            return null;
        }
        java.util.Hashtable<String,String> ret = new java.util.Hashtable<String,String>();
        BaseTokenizer tker = new BaseTokenizer(KnownConfiguration.CSSConfiguration.getInstance(),str);
        LexToken tk = null;
        while((tk = tker.nextToken()) != null)
        {
            if(tk.isName() == false && tk.isString() == false)
            {
                throw new SyntaxException(tk + ">> Only name allowed here.");
            }
            String name = tk.getToken();
            LexToken ts = tker.nextToken();
            if(null == ts || ts.isSpecial() == false || ts.equals(":") == false)
            {
                throw new SyntaxException(tk + ">> \":\" is needed followed the name");
            }
            StringBuffer value = new StringBuffer();
            LexToken tkV = null;
            while((tkV = tker.nextToken()) != null)
            {
                if(tkV.isSpecial() && tkV.equals(";"))
                {
                    break;
                }
                else
                {
                    value.append(tkV.getInitString());
                }
            }
            String v = value.toString().trim();
            if(v.length() > 0)
            {
                if(name.toLowerCase().trim().length()>0 && v.trim().length()>0)
                {
                    ret.put(name.toLowerCase(),v);
                }
            }
        }
        return ret;
    }
//    public static void main(String[] argvs)
//        throws LexException,SyntaxException
//    {
//        System.out.println(refinePart("width:100px;width:200px;"));
//    }
}
